package com.huawei.chengdu.meetingdemo.presenter;

import android.os.Message;

import com.huawei.chengdu.meetingdemo.model.AddrBookBean;
import com.huawei.chengdu.meetingdemo.util.AppContext;
import com.huawei.chengdu.meetingdemo.util.GlobalHandler;
import com.huawei.chengdu.meetingdemo.util.GlobalHandlerAdapter;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.view.IViewDataObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

public class RecordBookPresenter {
    private static final String TAG = RecordBookPresenter.class.getSimpleName();

    /**
     * 通讯录搜索列表
     */
    public CopyOnWriteArrayList<AddrBookBean> searchAddrBookUserList;

    /**
     * 加入会议 输入连想列表
     */
    public CopyOnWriteArrayList<AddrBookBean> searchSiteList;

    /**
     * 记录本的观察者列表
     */
    private Map<Integer, List<IViewDataObserver>> rbObservedList;

    public RecordBookPresenter() {
        LogUtil.debug(TAG + " RecordBookPresenter init");
        rbObservedList = new HashMap<>();
        initGlobalData();
    }

    private void initGlobalData() {
        searchSiteList = new CopyOnWriteArrayList<>();
        searchAddrBookUserList = new CopyOnWriteArrayList<>();
    }

    /**
     * 注册记录本的观察者，如果记录本发生了变化，就需要通知观察者列表。
     *
     * @param id  ID
     * @param obj 注册界面
     */
    public void registerAddressObserved(int id, IViewDataObserver obj) {
        List<IViewDataObserver> observerList = rbObservedList.get(id);

        if (observerList == null) {
            observerList = new ArrayList<>();
            rbObservedList.put(id, observerList);
        }
        if (!observerList.contains(obj)) {
            observerList.add(obj);
        }
    }

    /**
     * 反注册
     *
     * @param listener 界面类型
     */
    public synchronized void unregistDataChangeListenerObj(IViewDataObserver listener) {
        Iterator<Map.Entry<Integer, List<IViewDataObserver>>> iters = rbObservedList.entrySet().iterator();
        while (iters.hasNext()) {
            Map.Entry<Integer, List<IViewDataObserver>> entry = iters.next();

            List<IViewDataObserver> observers = entry.getValue();

            if (observers == null) {
                continue;
            }

            int loc = -1;
            for (int i = 0; i < observers.size(); i++) {
                if (listener == observers.get(i)) {
                    loc = i;
                    break;
                }
            }
            if (loc != -1) {
                observers.remove(loc);
            }
        }
    }

    /**
     * 处理数据变化了，通知界面数据进行更新
     *
     * @param id      消息ID
     * @param message 制定类型的观察者
     */
    public void noticeDataChange(int id, Message message) {
        // 取出指定类型的观察者列表
        List<IViewDataObserver> observerList = rbObservedList.get(id);
        // 没有观察者，就退出
        if (observerList == null || AppContext.instance().globalHandler == null) {
            return;
        }
        GlobalHandlerAdapter msgData;
        Message msg;
        for (IViewDataObserver uiView : observerList) {
            if (uiView == null) {
                continue;
            }
            if (message == null) {
                msgData = new GlobalHandlerAdapter(uiView, id, null);
            } else {
                msgData = new GlobalHandlerAdapter(uiView, id, message.obj);
            }
            msg = Message.obtain();
            msg.what = id;
            msg.obj = msgData;
            GlobalHandler msgHandler = AppContext.instance().globalHandler;
            msgHandler.sendMessage(msg);
        }
    }
}
